package com.thingple.rodbell.packet;

import android.util.Log;

import com.thingple.rodbell.cmd.HandleState;
import com.thingple.rodbell.listener.CarryStateListener;
import com.thingple.rodbell.listener.TagListener;


public abstract class AbstractPacketProcessor extends CmdHandler implements Runnable {

    private static final String TAG = AbstractPacketProcessor.class.getName();

    protected boolean work = false; // 默认自己不能执行，必须通过start

    protected CarryStateListener carryStateListener;
    protected TagListener tagListener;

    public AbstractPacketProcessor() {
        init();
    }

    private void init() {
        reset();
    }

    public void reset() {
        q.clear();
        buf.clear();
        state = HandleState.Start;
    }
    public void setTagListener(TagListener listener) {
        tagListener = listener;
    }
    public void setCarryStateListener(CarryStateListener listener) {
        carryStateListener = listener;
    }

    public void start() {
        if (work) {
            return;
        }
        work = true;
        new Thread(this).start();
    }

    public void stop() {
        Log.d(TAG, "Packet Processor stop");
        work = false;
        release();
    }

    protected void release() {
        tagListener = null;
        carryStateListener = null;

        q.clear();
        buf.clear();
    }
}
